package com.hong.chapter12;

import java.util.Arrays;

/**
 * ThreadGroupApi 相关方法测试
 */
public class ThreadGroupApi {
    public static void main(String[] args) throws InterruptedException{
        ThreadGroup tg1 = new ThreadGroup("TG1");
        Thread t1 = new Thread(tg1,"t1"){
            @Override
            public void run() {
    //            while (true){
                    try {
                        Thread.sleep(1_000);
                    }catch (InterruptedException e){
                        e.printStackTrace();
                            //break;
                    }
                //       }
            }
        };
        //tg1.setDaemon(true);
        t1.start();

        try {
            Thread.sleep(2_000);
            System.out.println(tg1.isDestroyed());
            tg1.destroy();
            System.out.println(tg1.isDestroyed());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    /*    ThreadGroup tg2 = new ThreadGroup(tg1,"TG2");
        Thread t2 = new Thread(tg2,"T2"){
            @Override
            public void run() {
                while (true){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        t2.start();
        //线程有几个
        System.out.println(tg1.activeCount());
        //线程组有几个
        System.out.println(tg1.activeGroupCount());
        t2.checkAccess();
        //tg1.destroy();
        System.out.println("======================");

        Thread[] ts1 = new Thread[tg1.activeCount()];
        tg1.enumerate(ts1);
        Arrays.asList(ts1).forEach(System.out::println);

        //System.out.println(ts1.length);
        System.out.println("======================");
        tg1.enumerate(ts1,true);
        Arrays.asList(ts1).forEach(System.out::println);

        System.out.println("======================");
        ts1 = new Thread[10];

        Thread.currentThread().getThreadGroup().enumerate(ts1,false);
        Arrays.asList(ts1).forEach(System.out::println);

        tg1.interrupt();
*/

    }
}
